package com.lhkj.ct.meta.modules.gauge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.constants.Constants;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.mybatis.core.metadata.SqlOrder;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.meta.modules.gauge.mapper.ScaleSetMapper;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScaleSet;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScaleSetRelation;
import com.lhkj.ct.meta.modules.gauge.service.ScaleSetRelationService;
import com.lhkj.ct.meta.modules.gauge.service.ScaleSetService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 测验组套 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-11-01
 */
@Service
public class ScaleSetServiceImpl extends IServiceImpl<ScaleSetMapper, TblScaleSet> implements ScaleSetService {

    @Resource
    private ScaleSetRelationService scaleSetRelationService;

    @Override
    public IPage<TblScaleSet> selectPages(TblScaleSet scaleSet, PageQuery pageQuery) {
        return getBaseMapper().selectPages(generatePage(pageQuery, SqlOrder.asc(TblScaleSet::getSort)),
                scaleSet);
    }

    @Override
    public IPage<TblScaleSet> selectPagesByAllot(TblScaleSet scaleSet, PageQuery pageQuery) {
        return getBaseMapper().selectPagesByAllot(generatePage(pageQuery),
                scaleSet);
    }

    @Override
    public TblScaleSet getForm(String id) {
        TblScaleSet scaleSet = getBaseMapper().selectById(id);
        if (scaleSet != null) {
            scaleSet.setScaleIds(scaleSetRelationService.selectBySetId(id));
        }
        return scaleSet;
    }

    @Override
    public String qrCode(String id) {
        Set<String> strings = getBaseMapper().selectByCombs(Collections.singletonList(id));
        Validate.notEmpty(strings, "该组套所包含的量表已删除或已禁用");
        UriComponentsBuilder redirectBuilder = UriComponentsBuilder.fromUriString(Constants.SERVICE_BASE_URL).path("/ct/api/plat/psychology/allot/scanAllot");
        redirectBuilder.queryParam("setId", id);
        redirectBuilder.queryParam("doctorId", ShiroUtil.getAuthUserInfo().getUserId());
        redirectBuilder.queryParam("deptId", ShiroUtil.getAuthUserInfo().getDeptId());
        return redirectBuilder.toUriString();
    }

    @Override
    @Transactional
    public int insert(TblScaleSet entity) {
        if (null == entity.getSort()) {
            Integer maxSort = getBaseMapper().selectMaxSort();
            entity.setSort(maxSort + 1);
        }
        int res = getBaseMapper().insert(entity);
        Set<TblScaleSetRelation> relations = entity.getScaleIds().stream().map(s -> new TblScaleSetRelation(entity.getId(), s)).collect(Collectors.toSet());
        scaleSetRelationService.saveBatch(relations);
        return res;
    }

    @Override
    public int updateById(TblScaleSet entity) {
        int res = getBaseMapper().updateById(entity);
        if (res == 0) {
            return 0;
        }
        scaleSetRelationService.getBaseMapper().delete(
                new LambdaQueryWrapper<TblScaleSetRelation>()
                .eq(TblScaleSetRelation::getCombId, entity.getId()));
        Set<TblScaleSetRelation> relations = entity.getScaleIds().stream().map(s -> new TblScaleSetRelation(entity.getId(), s)).collect(Collectors.toSet());
        scaleSetRelationService.saveBatch(relations);
        return res;
    }
}
